<?php


namespace app\api\controller;

use app\common\bean\ListMap;
use app\common\bean\StoreBean;
use app\common\bean\UserAddressBean;
use app\common\bean\UserBean;
use app\common\bean\UserImageBean;
use app\common\controller\ApiController;
use app\common\enum\UserAddressDefaultEnum;
use app\common\manage\StoreManage;
use app\common\manage\UserAddressManage;
use app\common\manage\UserImageManage;
use app\common\manage\UserManage;
use app\common\manage\WechatManage;
use app\common\model\StoreModel;
use app\common\model\UserAddressModel;
use app\common\utils\ExitJsonUtil;

class User extends ApiController
{
    protected $beforeActionList = [
        'decryptToken' => ['except' => 'test'],
    ];

    public function selectStore(){//用户选门店
        $userBean = new UserBean();
        $userBean->setStoreId($this->postParams['st_id']);
        $userBean->setId($this->userId);
        UserManage::getInstance()->edit($userBean);
        $store = StoreModel::getInstance()->selectData([StoreBean::$id=>$this->postParams['st_id']]);
        ExitJsonUtil::getInstance()->exitData($store);
    }
    public function addSetDefault() {//设置默认地址
        $userAddBean = new UserAddressBean();
        $userAddBean->setDefault(1);
        $userAddBean->setUserId($this->userId);
        UserAddressManage::getInstance()->editAll($userAddBean);
        $userAddBean->setData($this->postParams);
        $userAddBean->setDefault(2);
        UserAddressManage::getInstance()->edit($userAddBean);
        ExitJsonUtil::getInstance()->exitData([]);
    }

    /**
     * 获取用户默认地址
     * @throws \Exception
     */
    public function getDefaultAdd() {//设置默认地址
        $userAddBean = new UserAddressBean();
        $userAddBean->setUserId($this->userId);
        $userAddBean = UserAddressManage::getInstance()->getDefaultAddr($userAddBean);
        ExitJsonUtil::getInstance()->exitData($userAddBean->getData());
    }

    public function addDel() {//删除地址
        $userAddBean = new UserAddressBean();
        $userAddBean->setData($this->postParams);
        if ($info = UserAddressManage::getInstance()->getInfo($userAddBean)) {
            UserAddressManage::getInstance()->delete($userAddBean);
            ExitJsonUtil::getInstance()->exitData([]);
        }

    }

    public function addAdd() {//添加地址
        $userAddBean = new UserAddressBean();
        $userAddBean->setData($this->postParams);
        $userAddBean->setUserId($this->userId);
        $userAddBean->setCreatedAt(date('Y-m-d H:i:s'));
        if (!UserAddressModel::getInstance()->selectData([UserAddressBean::$userId => $this->userId])) {
            $userAddBean->setDefault(2);
        }
        $userAddBean->setDefault($userAddBean->getDefault() ? UserAddressDefaultEnum::ISDEFAULT : UserAddressDefaultEnum::NODEFAULT);
        UserAddressManage::getInstance()->add($userAddBean);
        ExitJsonUtil::getInstance()->exitData('添加地址成功');
    }

    /**编辑地址
     * @throws \Exception
     */
    public function editAdd() {
        $userAddBean = new UserAddressBean();
        $userAddBean->setData($this->postParams);
        $userAddBean->setUserId($this->userId);
        $userAddBean->setCreatedAt(date('Y-m-d H:i:s'));
        if (!UserAddressModel::getInstance()->selectData([UserAddressBean::$userId => $this->userId])) {
            $userAddBean->setDefault(2);
        }
        $userAddBean->setDefault(($userAddBean->getDefault() !== 'false') ? UserAddressDefaultEnum::ISDEFAULT : UserAddressDefaultEnum::NODEFAULT);
        UserAddressManage::getInstance()->edit($userAddBean);
        ExitJsonUtil::getInstance()->exitData('修改成功');
    }

    public function addrList() {//地址列表
        $list = UserAddressModel::getInstance()->selectData([UserAddressBean::$userId => $this->userId], true);
        ExitJsonUtil::getInstance()->exitData($list);
    }

    public function getUserInfo() {//查询用户信息
        $userBean = new UserBean();
        $userBean->setId($this->userId);
        $user = UserManage::getInstance()->getInfo($userBean);
        ExitJsonUtil::getInstance()->exitData($user->getData());
    }

    public function getImgsList() {//获取用户上传图片
        $listMap = new ListMap();
        $listMap->setParameter(UserImageBean::$userId, $this->userId);
        $imgs = UserImageManage::getInstance()->getImgList($listMap);
        ExitJsonUtil::getInstance()->exitData($imgs->getData());
    }

    public function uploadFile() {//上传文档
        $listMap = new ListMap();
        $listMap->setParameter(UserImageBean::$userId, $this->userId);
        $listMap->setParameter(UserBean::$appletOpenId, $this->openId);
        $userImageBean = UserImageManage::getInstance()->uploadFile($listMap);
        ExitJsonUtil::getInstance()->exitData($userImageBean->getData());
    }

    public function uploadImg() {//上传图片
        $listMap = new ListMap();
        $listMap->setParameter(UserImageBean::$userId, $this->userId);
        $userImageBean = UserImageManage::getInstance()->uploadImg($listMap);
        ExitJsonUtil::getInstance()->exitData($userImageBean->getData());
    }

    /**
     * @throws \Exception
     */
    public function delUploadImg() {//上传图片
        $listMap = new ListMap();
        $listMap->setData($this->postParams);
        $listMap->setParameter(UserImageBean::$userId, $this->userId);
        UserImageManage::getInstance()->batchDel($listMap);
        ExitJsonUtil::getInstance()->exitData('删除成功');
    }


    /**
     * 解析手机号码
     * @throws \Exception
     */
    public function mobilePhone() {
        $listMap = new ListMap();
        $listMap->setData($this->postParams);
        $code = $listMap->getParameter("code");
        $iv = $listMap->getParameter("iv");
        $encryptedData = $listMap->getParameter("encryptedData");
        $result = WechatManage::getInstance()->mobilePhone($code, $iv, $encryptedData);
        ExitJsonUtil::getInstance()->exitData($result->getData());
    }

    /**
     * 获取门店审核信息
     * @throws \Exception
     */
    public function getStoreApply() {
        $userBean = new UserBean();
        $userBean->setId($this->userId);
        $userBean = UserManage::getInstance()->getInfo($userBean);
        $result = StoreManage::getInstance()->getStoreApply($userBean);
        ExitJsonUtil::getInstance()->exitData($result);
    }

}